<template>
	<view class="login-container">
		<!-- 企微客服区域 -->
		<view class="customer-service">
			<image style="width: 750rpx; height: 605rpx;" src="/static/img_denglu2.png" mode="scaleToFill" />
			<view class="text-tab">
				<text>康养小程序</text>
			</view>

			<!-- 			<view class="customer-left">
				<text class="service-text">添加企微客服>>></text>
				<text class="hint-text">长按识别二维码</text>
			</view>
			<view class="customer-right">
				<image src="/static/img_denglu.png" mode="aspectFit" class="qrcode" />
				<text class="hint-text">客服二维码</text>
			</view> -->
		</view>

		<view class="login-tabs">
			<!-- 登录类型切换 -->
			<u-tabs :list="loginTypes" :current="currentTab" @change="changeLoginType" active-color="#03B8CD" item-width="50%"
				bg-color="#ffffff" :active-item-style="activeItemStyle" :inactiveStyle="{ color: '#999' }"
				itemStyle="height: 96rpx;"></u-tabs>

			<!-- 从业者登录注册 -->
			<!-- 表单区域 -->
			<view class="form-container" v-if="currentTab == 0">
				<!-- 手机号输入 -->
				<u-form :model="formData" ref="loginForm" :rules="rules">
					<u-form-item prop="phone" label="手机号：" label-width="160" label-position="top">
						<u-input v-model="formData.phone" placeholder="请输入手机号" clearable />
					</u-form-item>

					<!-- 验证码输入 -->
					<!-- 						<u-form-item prop="code" label="验证码：" label-width="160" v-if="pwsOrCode == 0" label-position="top">
							<u-input v-model="formData.code" placeholder="请输入短信验证码" clearable>
								<template #suffix>
									<u-button @click="getSmsCode" :disabled="isCounting" size="mini" type="primary" plain>
										{{ countdownText }}
									</u-button>
								</template>
								<u-button slot="right" type="success" size="mini" @click="getCode">{{codeTips}}</u-button>
							</u-input>
						</u-form-item> -->
					<u-form-item label-position="top" label="验证码" prop="code" label-width="160" v-if="pwsOrCode == 0">
						<u-input :border="false" placeholder="请输入验证码" v-model="formData.code" type="text"></u-input>
						<u-button slot="right" :hair-line="false" :custom-style="customStyle" type="default" size="mini"
							@click="getCode">{{codeTips}}</u-button>
					</u-form-item>
					<!-- 密码输入框 -->
					<u-form-item prop="code" label="密码：" label-width="160" label-position="top" v-else>
						<u-input type="password" v-model="formData.pwscode" placeholder="请输入密码" clearable>
							<template #suffix>
								<u-button @click="getSmsCode" :disabled="isCounting" size="mini" type="primary">
									{{ countdownText }}
								</u-button>
							</template>
						</u-input>
					</u-form-item>
				</u-form>

				<!-- 密码登录切换 -->
				<view class="switch-login" @click="switchToPassword">
					<text>{{pwsCodeName}}</text>
				</view>

				<!-- 协议勾选 -->
				<view class="agreement">
					<u-checkbox-group>
						<u-checkbox v-model="agreed" shape="circle" active-color="#03B8CD">
							阅读并同意《用户使用协议》
						</u-checkbox>
					</u-checkbox-group>
				</view>

				<!-- 登录按钮 -->
				<!-- 			<u-button type="primary" @click="handleLogin" :loading="isLogging" shape="circle">
						登录
					</u-button>
					<u-button type="primary" @click="getUserProfile" :loading="isLogging" shape="circle">
						绑定微信号
					</u-button> -->

				<!-- 登录按钮 -->
				<view class="loginbut">
					<u-button type="default" :custom-style="btuStyle" @click="handleLogin" :loading="isLogging" shape="circle">
						登录/注册
					</u-button>

				</view>

				<!-- 测试按钮 -->
<!-- 				<view class="regbug">
					<u-button type="default" @click="handleLoginTest" :loading="isLogging" shape="circle">
						登录Test
					</u-button>
				</view> -->

			</view>
			<!-- 从业者登录注册结束 -->

			<!-- 机构登录与注册 -->
			<!-- 表单区域 -->
			<view class="form-container" v-else>
				<!-- 手机号输入 -->
				<u-form :model="formData" ref="loginForm" :rules="rules">
					<u-form-item prop="phone" label="手机号：" label-width="160" label-position="top">
						<u-input v-model="formData.phone" placeholder="请输入手机号" clearable />
					</u-form-item>
<!-- 					<u-form-item prop="phone" label="链接：" label-width="160" label-position="top">
						<u-input v-model="formData.ywcode" placeholder="请输入ywcode" clearable />
					</u-form-item> -->

					<!-- 验证码输入 -->
					<!-- 						<u-form-item prop="code" label="验证码：" label-width="160" v-if="pwsOrCode == 0" label-position="top">
							<u-input v-model="formData.code" placeholder="请输入短信验证码" clearable>
								<template #suffix>
									<u-button @click="getSmsCode" :disabled="isCounting" size="mini" type="primary" plain>
										{{ countdownText }}
									</u-button>
								</template>
							</u-input>
						</u-form-item> -->
					<u-form-item label-position="top" label="验证码" prop="code" label-width="160" v-if="pwsOrCode == 0">
						<u-input :border="false" placeholder="请输入验证码" v-model="formData.code" type="text"></u-input>
						<u-button slot="right" :plain="true" :hair-line="false" :custom-style="customStyle" type="default"
							size="mini" @click="getCode">{{codeTips}}</u-button>
					</u-form-item>
					<!-- 密码输入框 -->
					<u-form-item prop="code" label="密码：" label-width="160" label-position="top" v-else>
						<u-input type="password" v-model="formData.pwscode" placeholder="请输入密码" clearable>
							<template #suffix>
								<u-button @click="getSmsCode" :disabled="isCounting" size="mini" type="primary">
									{{ countdownText }}
								</u-button>
							</template>
						</u-input>
					</u-form-item>
				</u-form>

				<!-- 密码登录切换 -->
				<view class="switch-login" @click="switchToPassword">
					<text>{{pwsCodeName}}</text>
				</view>

				<!-- 协议勾选 -->
				<view class="agreement">
					<u-checkbox-group>
						<u-checkbox v-model="agreed" shape="circle" active-color="#03B8CD">
							阅读并同意《用户使用协议》
						</u-checkbox>
					</u-checkbox-group>
				</view>

				<!-- 机构登录按钮 -->
				<view class="loginbut">
					<u-button type="default" :custom-style="btuStyle" @click="handleLogin" :loading="isLogging" shape="circle">
						登录
					</u-button>

				</view>

				<!-- 注册按钮 -->
				<view class="regbug">
					<u-button type="default" :custom-style="btuStyle" @click="handleReg" :loading="isLogging" shape="circle">
						申请入驻/注册
					</u-button>
				</view>


				<!-- 测试按钮 -->
<!-- 				<view class="regbug">
					<u-button type="primary" @click="handleLoginTest" :loading="isLogging" shape="circle">
						登录Test
					</u-button>
				</view> -->

			</view>
			<u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
			<!-- 机构登录注册结束 -->
		</view>

		<!-- 机构审核中与审核不通过时弹窗内容 -->
		<u-popup v-model="showPopup" mode="center" border-radius="20" width="80%">
			<view class="popup-container">
				<view class="popup-header" v-if="cur_status == 0 || cur_status == 1">
					<text class="title">已存在企业</text>
				</view>
				<view class="popup-header" v-if="cur_status == -1">
					<text class="title">企业不存在</text>
				</view>

				<view class="popup-content" v-if="cur_status == 0 || cur_status == 1">
					<view class="info-item">
						<text class="label">企业名称：</text>
						<text class="value">{{ companyInfo.name }}</text>
					</view>

					<view class="info-item">
						<text class="label">经营地址：</text>
						<text class="value">{{ companyInfo.address }}</text>
					</view>

					<view class="info-item">
						<text class="label">行业类型：</text>
						<text class="value">{{ companyInfo.industry }}</text>
					</view>

					<view class="info-item">
						<text class="label">入驻状态：</text>
						<text class="value status-fail">{{companyInfo.status}}</text>
					</view>

					<view class="info-item feedback">
						<text class="label">反馈原因：</text>
						<text class="value">{{ companyInfo.feedback }}</text>
					</view>
				</view>
				<view class="" v-else-if="cur_status == -1">
					<text class="label">当前机构不存在,请先申请入驻/注册机构</text>
				</view>
				<!-- 审核中不能修改 -->
				<view class="popup-footer" v-if="cur_status == 0 || cur_status == -1">
					<u-button class="footer-btn" type="default" @click="handleModify">我知道了</u-button>
				</view>
				<!-- 只有审核不通过才能修改 -->
				<view class="popup-footer-foot" v-if="cur_status == 1">
					<u-button class="footer-btn" type="default" @click="handleCancel">取消</u-button>
					<u-button class="footer-btn" type="default" @click="handleModify" :custom-style="btuStyle">修改信息</u-button>
				</view>


			</view>
		</u-popup>

	</view>

</template>

<script>
	export default {
		data() {
			return {
				// 登录状态(若本地存在Token，usercode)
				userToken: '',
				userCode: '',
				// 顶部
				title: '康养小程序',
				backText: '返回',
				backIconName: 'nav-back',
				background: {
					// 'background-image': 'linear-gradient(45deg, rgb(98, 209, 227), rgb(100, 224, 225))'
					'background-image': 'url(/static/img_denglu2.png)'
					// 'background-image': 'url(https://www.xzbky.com/static/kangyangtrain/1000/img_denglu2.png)'
				},
				isBack: false,
				isFixed: true,
				isbottom: false,
				// 登录类型切换
				loginTypes: [{
						name: '从业者登录'
					},
					{
						name: '企业/机构登录'
					}
				],
				currentTab: 0,
				// 密码或验证码
				pwsOrCode: 0,
				pwsCodeName: '使用密码登录 >',
				// 短信验证码
				codeTips: '',
				// 表单数据
				formData: {
					phone: '',
					code: '',  ///验证码
					pwscode: '',
					ywcode:'',
				},

				// 验证码倒计时
				isCounting: false,
				countdown: 60,
				customStyle: {
					// "backgroundColor": "#03B8CD"
					"color": "#03B8CD"
				},
				btuStyle: {
					"backgroundColor": "#03B8CD",
					"color": "#ffffff"
				},

				// 协议勾选
				agreed: false,

				// 登录状态
				isLogging: false,
				activeItemStyle: {
					backgroundColor: '#ffffff', // 设置活动标签的背景颜色为灰色
					fontSize: '36rpx',
					color: '#03B8CD', // 设置活动标签的文字颜色
					fontWeight: 'bold'
				},
				// 微信信息
				userInfo: {},
				wxcode: '2636e646cb51b89a59c6d3150e245253', ///微信wxcode
				encryptedData: '',
				iv: '',
				rawData: '',
				signature: '',
				// 弹空显示
				modalshow: false,
				showTitle: true,
				content: '',
				urloptions:'',
				// 审核中与审核不通过时的弹窗
				btuStyle: {
					"backgroundColor": "#03B8CD",
					"color": "#ffffff"
				},
				showPopup: false,
				cur_status: -1,
				companyInfo: {
					name: "示例企业名称",
					address: "北京市朝阳区某某路123号",
					industry: "互联网/信息技术",
					status:'',   ///审核状态
					feedback: "CCCCC"
				},
				// 手机号与密码输入框验证
				rules: {
					phone: [{
							required: true,
							message: '手机号不能为空',
							trigger: ['blur', 'change']
						},
						{
							validator: (rule, value, callback) => {
								return this.$u.test.mobile(value);
							},
							message: '手机号格式不正确',
							trigger: ['blur', 'change']
						}
					],
					code: [{
							required: true,
							message: '验证码不能为空',
							trigger: ['blur', 'change']
						},
						{
							pattern: /^\d{6}$/,
							message: '验证码必须是6位数字',
							trigger: ['blur', 'change']
						}
					],
					password: [{
							required: true,
							message: '密码不能为空',
							trigger: ['blur', 'change']
						},
						{
							min: 6,
							message: '密码长度不能小于6位',
							trigger: ['blur', 'change']
						},
						{
							validator: (rule, value, callback) => {
								// 密码复杂度：至少包含字母和数字
								return /^(?=.*[A-Za-z])(?=.*\d).+$/.test(value);
							},
							message: '密码必须包含字母和数字',
							trigger: ['blur', 'change']
						}
					]
				},
				yqm: '',
				linkcode: '',
				ywcode: '',
			}
		},
		computed: {
			countdownText() {
				return this.isCounting ? `${this.countdown}s后重试` : '获取验证码'
			}
		},
		// 若已经登录过，存在token就自动登录
		onLoad(options) {		
			// 接收邀请链接的内容
			let stringParams = JSON.stringify(options)
			// 转成对象
			this.urloptions = JSON.parse(stringParams)

			// 将获取的url进行转码
			// const query = decodeURIComponent(options);
			// const params = new URLSearchParams(query);
			this.yqm = this.urloptions.yqm;      // 获取邀请码
			this.linkcode = this.urloptions.linkcode;// 获取控制码
			this.ywcode = this.urloptions.ywcode ///业务码	
			
			// 暂时注释
			uni.setStorageSync('yqm', this.yqm)
			uni.setStorageSync('ywcode', this.ywcode)
			uni.setStorageSync('linkcode', this.linkcode)
			// console.log('参数', this.yqm)
			// console.log('参数-1', this.linkcode)
			// console.log('参数-2', this.ywcode)

			// 暂时在每次打开时，在本地添加一个wxcode
			// uni.setStorageSync('wxcode', '2636e646cb51b89a59c6d3150e245253');
			this.userToken = uni.getStorageSync('token');
			this.userCode = uni.getStorageSync('usercode')
			// console.log('查询用户信息-1：', res)	

			
	
		},
		onShow() {
			// 返回时初始化Loading
			this.isLogging = false
			
			// 判断是否默认登录,登录过的进入
			if (this.userToken != '' && this.usercode != '') {
				this.$u.api.queryuserinfo({
					usercode: this.userCode
				}).then(res => {
					console.log('查询用户信息：', res)
					// ywcode:  团队邀请考证报名：kzbm  ,  比赛邀请： bsbm   直播：zbbm   课程：kcbm
					if (res.data.base_info.user_type == 1) {
						// 邀请进入时的跳转
						this.wangyeJump()
	
					} else {
						// 机构端首页
						uni.switchTab({
							url: '/pages/instTabBar/invite',
						})
					}						
				})
			}		
			
			
			this.$u.toast('测试：',uni.getStorageSync('ywcode') );
			setTimeout(() => {
				this.formData.ywcode = 'ywcode:'+uni.getStorageSync('ywcode')+',linkcode:'+uni.getStorageSync('linkcode')
			},1000)
			
			setTimeout(() => {
				this.$u.api.recordlog({
					eventname: '获取链接', ///事件名称
					eventinfo: {"urlyqm":this.urloptions,"ywcode": this.formData.ywcode}  ///事件描述，可以是字典，可以是文本
				}).then(res=>{
					console.log('获取链接', this.urloptions)			
						console.log('获取链接-1', this.linkcode,this.ywcode,this.yqm)		
				})
			},2000)

		},

		methods: {
			// 邀请进入时页面跳转的方法
			wangyeJump() {
				let ywcode = uni.getStorageSync('ywcode')
				if(ywcode == 'kzbm') {
					// 从业者邀请报名
					uni.navigateTo({
						url: '/pages/Practitioner/examTrain/examTrain'
					})
				} else if(ywcode == 'zbbm') {
					// 直播详情
					uni.navigateTo({
						url: '/pages/Practitioner/teacherLive/teacherLive'
					})
				} else if(ywcode == 'bsbm') {
					// 比赛详情
					uni.navigateTo({
						url: '/pages/Practitioner/prizeFight/prizeFight'
					})
				} else if(ywcode == 'kcbm') {
					// 课程
					uni.navigateTo({
						url: '/pages/Practitioner/onlinecourse/courselist'
					})
				} else {
					// 从业者端首页
					uni.navigateTo({
						url: '/pages/Practitioner/learning/learning'
					})
				}	
			},
			
			codeChange(text) {
				this.codeTips = text;
			},
			// 获取验证码
			getCode() {
				if (!this.$u.test.mobile(this.formData.phone)) {
					return this.$u.toast('请输入正确的手机号')
				}

				if (this.$refs.uCode.canGetCode) {
					// 向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码',
						mask: true
					})
					setTimeout(() => {
						this.$u.api.createyzm({
							"userphoneno": this.formData.phone
						}).then(res => {
							console.log('验证码：',res)
							let baseinfo = res.data.base_info
							if(baseinfo) {
								uni.setStorageSync('phoneCode', baseinfo)
							}
							uni.hideLoading();
							// 这里此提示会被this.start()方法中的提示覆盖
							this.$u.toast('验证码已发送');
							// 通知验证码组件内部开始倒计时
							this.$refs.uCode.start();
						}).catch((err) => {
							this.$u.toast('验证码获取失败，请重新获取！');
						})
					}, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			// 切换登录类型
			changeLoginType(index) {
				this.currentTab = index
				this.agreed = false
				this.formData.phone = '';
				this.formData.code = '';
				this.formData.pwscode = '';
				// 这里可以添加不同类型登录的逻辑
			},

			// 获取短信验证码
			getSmsCode() {
				if (!this.$u.test.mobile(this.formData.phone)) {
					console.log('手机号不对')
					return this.$u.toast('请输入正确的手机号')
				}

				this.isCounting = true
				this.startCountdown()

				// 调用获取验证码接口
				this.$u.api.sendSmsCode({
					phone: this.formData.phone,
					type: this.currentTab === 0 ? 'worker' : 'enterprise'
				}).then(() => {
					this.$u.toast('验证码已发送')
				}).catch(() => {
					this.isCounting = false
				})
			},

			// 倒计时逻辑
			startCountdown() {
				const timer = setInterval(() => {
					if (this.countdown <= 1) {
						clearInterval(timer)
						this.isCounting = false
						this.countdown = 60
					} else {
						this.countdown--
					}
				}, 1000)
			},

			// 切换到密码登录
			switchToPassword() {
				// uni.navigateTo({
				// 	url: '/pages/login/password'
				// })
				// pwsOrCode为0表示按钮“密码”，1按钮为“验证码”
				if (this.pwsOrCode == 0) {
					this.pwsCodeName = '使用验证码登录 >';
					this.pwsOrCode = 1;
					this.formData.code = ''

				} else {
					this.pwsCodeName = '使用密码登录 >';
					this.pwsOrCode = 0;
					this.formData.pwscode = ''
				}
			},

			//测试按钮
			handleLoginTest() {
				if (this.currentTab == 0) {
					uni.navigateTo({
						url: '/pages/Practitioner/learning/learning'
						// url: '/pages/Practitioner/instiVisitPract/instiVisitPract'
						// url: '/pages/Institution/settleIn/index'
					})
				} else {
					uni.switchTab({
						url: '/pages/instTabBar/invite',
						// url: '/pages/Institution/settleIn/index'
					})
				}
			},

			// 从业者/机构登录入口
			handleLogin() {				
				this.$refs.loginForm.validate(valid => {
					console.log('login', valid)
					if (valid) {
						// 先检查手机号，再验证密码与验证码
						if (!this.$u.test.mobile(this.formData.phone)) {
							return this.$u.toast('手机号或格式不对！')
						}
						if (this.pwsOrCode == 0) {
							if (!this.$u.test.code(this.formData.code, 6) ) {
								return this.$u.toast('验证码不对')
							}
						} else {
							if (this.formData.pwscode.length < 6 && this.formData.pwscode.length > 12) {
								return this.$u.toast('密码6-12位数')
							}
						}
						if (!this.agreed) {
							
							return this.$u.toast('请先阅读并同意用户协议')
						}
						this.isLogging = true
						// 以上输入内容合格
						// 点击登录，先用手机号查询信息，看是从业者还是机构者	
						// 先查询用户类型，再判断手机、密码或验证码是否正确
						this.$u.api.queryusertypebyphoneno({
							phoneno: this.formData.phone
						}).then(res => {
							let userType = res.data.base_info
							console.log('login-type-1:',res, this.formData.phone)	
							if(userType == 0){
								// 若为0，则表示用户不存在（需验证码确认才能创建用户）
								this.loginType()	
									// return this.$u.toast('请在从业者入口登录')
							} else if(userType == 1 || userType == null) {
								// 判断是否在从业者入口登录的，若不是提醒，若是判断密码或密码码是否正确，进入
								if(this.currentTab != 0){
									return this.$u.toast('请在从业者入口登录')
								} else {
									this.loginType()
								} 
							} else if(userType == 2) {
								// 判断是否在机构入口登录的，若不是提醒，若是判断密码或密码码是否正确，进入
								if(this.currentTab != 1){
									return this.$u.toast('请在机构入口登录')
								} else {
									console.log('login-type-logintype:',res, userType)	
									this.loginType()
								} 
							}
							this.isLogging = false							 
						}).catch(err => {
							return this.$u.toast('登录信息获取失败,请检查网络情况！')
							this.isLogging = false
						})
						// 这里可以添加表单提交逻辑
					} else {
						return this.$u.toast('验证失败')
						this.isLogging = false
						
					}
				});
			},
			
			// 根据类型判断从哪个入口登录
			loginType() {
				if (this.currentTab == 0) {
					// 获取微信code，然后请求服务端成功，即登录跳转	
					uni.setStorageSync('userType', 1)
					this.getWxLoginCode()
				} else {
					//机构登录
					// 1、机构登录使用验证码或密码，并需要查询机构入驻状态
					uni.setStorageSync('userType', 2)
					this.queryAndLogin()	
					// 获取微信code，然后请求服务端成功，即登录跳转，登录同时需要判断机构是否存在
					// this.getWxLoginCode()
				}
			},
			
			// 根据手机号获取从业者用户信息
			// queryuserinfobyphoneno() {
			// 	this.$u.api.queryuserinfobyphoneno({
			// 		phoneno: this.formData.phone
			// 	}).then(res => {
			// 		console.log('根据手机号查询用户：', res)
			// 	})
			// },
			
			// 根据手机号获取机构企业信息
			// queryjguser() {
			// 	this.$u.api.queryjgqyxxbyphoneno({
			// 		phoneno: this.formData.phone
			// 	}).then(res => {
			// 		console.log('获取机构企业信息', res);
			// 	})
			// },
			

			// 机构登录使用验证码或密码，并需要查询机构入驻状态
			queryAndLogin() {
				this.$u.api.queryjguser({
					phoneno: this.formData.phone
				}).then(res => {
					console.log('queryAndLogin', res)
					let baseInfo = res.data.base_info
					// 判断对象是否为空
					if (Object.keys(baseInfo).length === 0) {
						this.isLogging = false
						this.cur_status = -1
						this.showPopup = true;
					} else {
						// 判断当前审核的状态,提示弹窗的按钮不一样，若审核状态为通过，则直接进入主页，不显示弹窗
						// 当审核中时处理(当前状态，0待审核，1审核失败，2审核通过，不传查询所有)
						if (baseInfo.base.cur_status == 0) {
							// 先获取信息
							this.companyInfo.name = baseInfo.qyxx.qyxx_name;
							this.companyInfo.address = baseInfo.qyxx.qyxx_addr+baseInfo.qyxx.qyxx_realaddr;
							this.companyInfo.industry = baseInfo.qyxx.qyxx_jyzt;
							this.companyInfo.status = baseInfo.base.flow_msg;
							this.companyInfo.feedback = "CCCCC"	
							this.cur_status = 0
							this.showPopup = true;
						} else if (baseInfo.base.cur_status == 1) {
							// 当审核不通过时处理，可能需要修改
							this.cur_status = 1
							uni.setStorageSync('flowid', baseInfo.base.flow_id)
							this.companyInfo.name = baseInfo.qyxx.qyxx_name;
							this.companyInfo.address = baseInfo.qyxx.qyxx_addr+baseInfo.qyxx.qyxx_realaddr;
							this.companyInfo.industry = baseInfo.qyxx.qyxx_jyzt;
							this.companyInfo.status = '不通过';
							this.companyInfo.feedback = "CCCCC"	
							this.showPopup = true;
						} else {
							// 审核通过，需要调用登录接口，进行登录判断							
							this.showPopup = false;
							// 机构登录
							this.apiJgLogin()
							// let url = 'http://81.71.0.172:30100/api/login/jglogin';
							// this.sendTocylogin(url, {
							// 	phoneno: this.formData.phone,
							// 	wxcode: this.wxcode,
							// 	userpwd: this.formData.pwscode,
							// 	usersmsno: this.formData.code
							// });
							// uni.switchTab({
							// 	url: `/pages/instTabBar/invite?flowid={baseInfo.flow_id}`,
							// })
						}
					}

				})
			},

			// 提示的UModal提示窗，确认按钮
			modalConfirm() {
				this.modalshow = false
			},

			// 获取微信CODE
			getWxLoginCode() {
				console.log('登录getWxLoginCode：')
				uni.login({
					provider: 'weixin',
					success: (res) => {
						this.wxcode = res.code; // 获取code
						console.log('登录code：', this.wxcode)
						// 将code和加密数据传给后端
						if (this.currentTab == 0) {
							// 判断验证码是否正确
							if (this.pwsOrCode == 0) {
								if (this.formData.code === uni.getStorageSync('phoneCode')) {
									this.apiCyLogin()
								} else {
									this.$u.toast('验证码不正确！');
								}
							} else {
								this.apiCyLogin()								
							}							
						} else {							
							// 判断验证码是否正确
							if (this.pwsOrCode == 0) {
								if (this.formData.code === uni.getStorageSync('phoneCode')) {
									this.apiJgLogin()
								} else {
									this.$u.toast('验证码不正确！');
								}
							} else {
								this.apiJgLogin()						
							}	
						}
						this.isLogging = false
					},
					fail: (err) => {
						console.error('登录失败:', err);
						this.isLogging = false
					}
				});
			},
			
			// 从业者登录接口调用 
			apiCyLogin() {					
				this.$u.api.cylogin({
					"phoneno": this.formData.phone,
					"wxcode": this.wxcode,
					"userpwd": this.formData.pwscode,
					"usersmsno": this.formData.code,
					"useryqm": uni.getStorageSync('yqm'), //#用户邀请码
					"bizcode": uni.getStorageSync('ywcode'),//#邀请的业务码，根据前端定义的来分类，可以简单的用数值或者简称，比如机构考证邀请
					"useryqmurl": uni.getStorageSync('linkcode'),//#邀请的链接						
				}).then(res => {
					console.log('cylogin-info:', res);
					// 从业者登录
					let token = res.data.accesstoken;
					let usercode = res.data.usercode;
					let wxopenid = res.data.wxopenid;
					
					if (token != '' && usercode != '') {
						// token为真，存储到本地, 授权码与token存在，即用户存在
						uni.setStorageSync('token', token);
						uni.setStorageSync('usercode', usercode);
						uni.setStorageSync('phone', this.formData.phone)
						uni.setStorageSync('wxopenid', wxopenid)
						
						
						if(uni.getStorageSync('ywcode') == '') {
							uni.navigateTo({
								url: '/pages/Practitioner/learning/learning'
							})
						} else {
							// 判断是邀请进入时，直接跳转到相应的页面
							this.wangyeJump()
						}

					} else {
						this.isLogging = false
						this.$u.toast('登录失败或用户不存');
					}	
				}).catch((err) => {
					this.$u.toast('手机号或密码不正确！');
					this.isLogging = false
				})
				
			},
			
			// 机构端登录
			apiJgLogin() {
				this.$u.api.jglogin({
					"phoneno": this.formData.phone,
					"wxcode": this.wxcode,
					"userpwd": this.formData.pwscode,
					"usersmsno": this.formData.code
				}).then(res => {
					console.log('jglogin-info:', res);
					// 机构登录
					let token = res.data.accesstoken;
					let usercode = res.data.usercode;
					let wxopenid = res.data.wxopenid;
					
					if (token != '' && usercode != '') {
						// token为真，存储到本地, 授权码与token存在，即用户存在
						uni.setStorageSync('token', token);
						uni.setStorageSync('usercode', usercode);
						uni.setStorageSync('phone', this.formData.phone)
						uni.setStorageSync('wxopenid', wxopenid)
						
						
						if(uni.getStorageSync('ywcode') == '') {
							uni.switchTab({
								url: '/pages/instTabBar/invite'
							})
						} else {
							// 判断是邀请进入时，直接跳转到相应的页面
							this.wangyeJump()
						}
		
					} else {						
						this.$u.toast('机构登录失败或用户不存,请先申请入驻！');
					}	
					this.isLogging = false
				}).catch((err) => {
					this.$u.toast('手机号或密码不正确！');
					this.isLogging = false
				})
			},
			

			// 从业者/机构登录接口调用
			// sendTocylogin(url, data) {
			// 	console.log('sendTo:', data)
			// 	uni.request({
			// 		url: url,
			// 		method: 'POST',
			// 		data: data,
			// 		success: (res) => {
			// 			console.log('cylogin-info:', res);
			// 			// 从业者登录
			// 			let token = res.data.data.accesstoken;
			// 			let usercode = res.data.data.usercode;

			// 			if (token != '' && usercode != '') {
			// 				// token为真，存储到本地, 授权码与token存在，即用户存在
			// 				uni.setStorageSync('token', token);
			// 				uni.setStorageSync('usercode', usercode);
			// 				if (this.currentTab == 0) {
			// 					uni.navigateTo({
			// 						url: '/pages/Practitioner/learning/learning'
			// 					})
			// 				} else {
			// 					uni.switchTab({
			// 						url: '/pages/instTabBar/invite'
			// 					})
			// 				}
			// 			} else {
			// 				this.isLogging = false
			// 				this.$u.toast('登录失败或用户不存在');
			// 			}
			// 		},
			// 		fail: (err) => {
			// 			this.isLogging = false
			// 			console.error('登录失败或用户不存在：', err);
			// 		}
			// 	});
			// },

			// 获取当前登录人员的真实微信名与头像
			// 获取登录登录的code,encryptedData,iv
			// 获取用户信息
			getUserProfile() {
				uni.getUserProfile({
					desc: '用于完善会员资料',
					lang: 'zh_CN',
					success: (res) => {
						console.log('用户信息userinfo:', res);
						this.encryptedData = res.encryptedData;
						this.iv = res.iv;
						this.rawData = res.rawData;
						this.signature = res.signature;
						this.wxlogin();
					},
					fail: (err) => {
						console.error('获取用户信息失败:', err);
					}
				});
			},


			// 第一步：绑定微信openid时使用此获取微信登录code
			wxlogin() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						this.code = res.code; // 获取code
						console.log('登录code：', this.code)
						// 将code和加密数据传给后端
						this.sendToBackend({
							logincode: this.code,
							encryptedData: this.encryptedData,
							iv: this.iv,
							// rawData: this.rawData,
							// signature: this.signature
						});

						// this.getUserProfile(); // 获取用户信息
						// uni.request({
						// 	// url: 'https://tools.mimeauto.work/kangyangtrain/wx/get_wxinfo',
						// 	url: 'https://tools.mimeyummy.fun/kangyangtrain/wx/get_wxinfo',
						// 	method: 'GET',
						// 	data: {
						// 		logincode: this.code,
						// 	},
						// 	success: (res) => {
						// 		console.log('wxlogin:', res);
						// 	}
						// })
					},
					fail: (err) => {
						console.error('登录失败:', err);
					}
				});
			},


			// 第三步：将数据发送到后端解密
			sendToBackend(data) {
				console.log('sendTo:', data)
				uni.request({
					url: 'https://tools.mimeyummy.fun/kangyangtrain/wx/get_wxinfo',
					method: 'GET',
					data: data,
					success: (res) => {
						if (res.data.success) {
							this.userInfo = res.data.userInfo; // 获取真实用户信息
							console.log('login-info:', this.userInfo);
						}
					},
					fail: (err) => {
						console.error('获取用户信息失败：', err);
					}
				});
			},


			// 机构注册
			handleReg() {
				// this.isLogging = true
				// navigateTo带头部返回，即新页面入栈; redirectTo当前页面出栈，新页面入栈
				uni.navigateTo({
					url: '/pages/Institution/settleIn/index'
				})
				// console.log("this.route")
				// this.$u.route({
				//  url:'/pages/invite/invite'
				// })
			},

			//审核通过或不通过时的弹窗事件
			handleCancel() {
				this.showPopup = false
				this.isLogging = false
			},
			handleModify() {
				this.showPopup = false
				this.isLogging = false
				// 实际项目中跳转到修改页面
				// uni.navigateTo({ url: '/pages/modify/index' })
				// uni.navigateTo({
				// 	url: `/pages/Institution/settleIn/index?phoneno=${this.formData.phone}`
				// })
			}

		}
	}
</script>

<style lang="scss" scoped>
	.login-container {
		// padding: 40rpx;
		background-color: #F1F7F7;
		font-size: 28rpx;
		color: #606266;


		/deep/ .u-tab-item {
			// border-radius: 10px 10px 0 0;
			// background-color: red;
		}

		uni-vie {
			// border-radius: 10px 10px 0 0;
		}

		.customer-service {
			// display: flex;
			// flex-direction: row;
			width: 100%;
			align-items: center;
			// margin-bottom: 60rpx;

			.text-tab {
				position: fixed;
				top: 110rpx;
				left: 288rpx;
				font-size: 38rpx;
			}

			.customer-left {
				display: flex;
				flex-direction: column;
				font-size: 32rpx;
				font-weight: bold;
				margin-bottom: 10rpx;
				margin-right: 60px;
				margin-left: 20px;
			}

			.customer-right {
				display: flex;
				flex-direction: column;
				float: right;
				align-items: center;

			}

			.hint-text {
				font-size: 24rpx;
				color: $u-tips-color;
				margin: 10rpx 0;
			}

			.qrcode {
				width: 200rpx;
				height: 200rpx;
				margin: 20rpx 0;
			}
		}

		.login-tabs {
			position: absolute;
			top: 526rpx;
			left: 0rpx;
			right: 0rpx;
			// background-color: #F1F7F7;
			background-color: rgba(255, 255, 255, 0.8);
			/* 黑色30%透明度 */
			border-radius: 32rpx 32rpx 0 0;
			width: 100%;
			height: 1099rpx;
			/* 确保子元素也遵循圆角 */
			overflow: hidden;

			/* 修改u-tabs内部样式 */
			/deep/ .u-tabs {
				// background-color: transparent !important;
				background-color: #1fe0 !important;
				/* 完全透明简写 */

				.u-tabs__wrapper {
					// background-color: transparent !important;
					background-color: #1fe0 !important;
					/* 完全透明简写 */
				}

				.u-tabs__item {
					// background-color: transparent !important;
					background-color: #1fe0 !important;
					/* 完全透明简写 */

					&.u-tabs__item--active {
						// background-color: #fff !important;
						background-color: #1fe0 !important;
						/* 完全透明简写 */
						// border-radius: 20rpx 20rpx 0 0;
					}
				}
			}

		}

		.form-container {
			// margin-top: 40rpx;
			margin: 60rpx 62rpx 0 62rpx;



			.switch-login {
				text-align: left;
				color: "blue";
				font-size: 26rpx;
				margin: 20rpx 0;
			}

			.agreement {
				margin: 80rpx 0 50rpx;
				display: flex;
				align-items: center;
			}


			.loginbut {
				margin-bottom: 20px;
			}

			.regbug {
				margin-bottom: 10px;
			}
		}

		/deep/ .u-size-default {
			width: 622rpx;
			height: 96rpx;
		}

		// 机构登录与查询时弹窗
		.popup-container {
			padding: 30rpx;
		}

		.popup-header {
			// display: flex;
			// justify-content: space-between;
			// align-items: center;
			padding-bottom: 20rpx;
			border-bottom: 1rpx solid #eee;
			margin-bottom: 20rpx;
			text-align: center;

			.title {
				font-size: 32rpx;
				font-weight: bold;
				color: #333;
			}
		}

		.popup-content {
			padding: 10rpx 0;
		}

		.info-item {
			display: flex;
			// margin-bottom: 25rpx;
			line-height: 1.6;
			font-size: 28rpx;

			.label {
				color: #666;
				width: 160rpx;
				flex-shrink: 0;
			}

			.value {
				color: #333;
				flex: 1;
			}

			&.feedback {
				align-items: flex-start;
			}
		}

		.status-fail {
			color: #fa3534 !important;
		}

		.popup-footer {
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;

			// .footer-btn {
			// 	width: 250rpx
			// }
			
			// 此样式主要为了适配wx小程序的按钮
			/deep/ .u-size-default {
				align-items: center;
				width: 550rpx;
			}
		}
		.popup-footer-foot {
			display: flex;
			justify-content: space-between;
			margin-top: 30rpx;
			
			// 此样式主要为了适配wx小程序的按钮
			/deep/ .u-size-default {
				align-items: center;
				width: 270rpx;
			}
		}
	}
</style>